﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.SystemManage
{
    public partial class BonusPlanChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrp();
                LoadData();
            }
        }
        #endregion
        protected void BindDrp()
        {
            List<MemType> mt = SDDbSession.Default.From<MemType>().ToList();
            drp_type.DataSource = mt;
            drp_type.DataTextField = "Value";
            drp_type.DataValueField = "MemTypeID";
            drp_type.DataBind();
        }

        #region 读取数据


        private void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                BounsPlan Tm = SDDbSession.Default.From<BounsPlan>().Where(BounsPlan._.BounsPlanID == id).ToFirst();
                txt_Name.Text = Tm.Name;
                txt_day.Text = Tm.DayNumber.ToString();
                txt_price.Text = Tm.Income.ToString();
                drp_type.SelectedValue = Tm.MemTypeID.ToString();
                txt_times.Text = Tm.Times.ToString();
            }
        }
        #endregion

        #region 保存数据
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            BounsPlan Tm = new BounsPlan();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Tm.BounsPlanID = int.Parse(Request["id"]);
                Tm.Attach();
                Tm.Name = txt_Name.Text;
                Tm.Income = string.IsNullOrEmpty(txt_price.Text) ? 0 : decimal.Parse(txt_price.Text);
                Tm.DayNumber = string.IsNullOrEmpty(txt_day.Text) ? 0 : int.Parse(txt_day.Text);
                Tm.MemTypeID = int.Parse(drp_type.SelectedValue);
                Tm.Times = string.IsNullOrEmpty(txt_times.Text) ? 0 : int.Parse(txt_times.Text);
                returnvalue = SDDbSession.Default.Update<BounsPlan>(Tm);
            }
            else
            {
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                Tm.DeAttach();
                Tm.Name = txt_Name.Text;
                Tm.Income = string.IsNullOrEmpty(txt_price.Text) ? 0 : decimal.Parse(txt_price.Text);
                Tm.DayNumber = string.IsNullOrEmpty(txt_day.Text) ? 0 : int.Parse(txt_day.Text);
                Tm.MemTypeID = int.Parse(drp_type.SelectedValue);
                Tm.Times =0;
                Tm.Money = 0;
                returnvalue = SDDbSession.Default.Insert<BounsPlan>(Tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        #endregion
    }
}